1 민주당 경선 데이터

민주당 보도자료를 통해서 제 20 대 대선 후보자 선출을 위한 민주당 경선 데이터를 얻을 수 있다.

2 정형데이터

library(tidyverse)

library(tidyverse)
library(readxl)
library(testthat)

race_raw <- read_excel("data/2021_민주당 경선결과.xlsx", sheet = "Sheet1", skip =1)

race_tbl <- race_raw %>% 
  janitor::clean_names(ascii = FALSE) %>% 
  filter(!str_detect(x1, pattern = "^\\(")) %>% 
  select(!contains("합계"))

type_01 <- race_tbl %>% 
  select(시도 = x1, 이재명_3, 이낙연_4, 박용진_5, 추미애_6) %>% 
  filter(!str_detect(시도, "누적")) %>% 
  drop_na()

type_02 <- race_tbl %>% 
  select(시도 = x1, 이재명_8, 이낙연_9, 박용진_10, 추미애_11) %>% 
  filter(!str_detect(시도, "누적")) %>% 
  drop_na()

type_03 <- race_tbl %>% 
  select(시도 = x1, 이재명_13, 이낙연_14, 박용진_15, 추미애_16) %>% 
  filter(!str_detect(시도, "누적")) %>% 
  drop_na()

type_04 <- race_tbl %>% 
  select(시도 = x1, 이재명_18, 이낙연_19, 박용진_20, 추미애_21) %>% 
  filter(!str_detect(시도, "누적")) %>% 
  drop_na()

electoral_college <- race_tbl %>% 
  select(시도 = x1, 선거인단) %>% 
  filter(!str_detect(시도, "누적")) %>% 
  drop_na()

## 데이터 정규화

type_01_tbl <- type_01 %>% 
  pivot_longer(-시도, names_to = "후보자명", values_to = "득표") %>% 
  mutate(후보자명 = str_remove(후보자명, pattern = "_[0-9]+$")) %>% 
  mutate(구분 = "권리당원")

type_02_tbl <- type_02 %>% 
  pivot_longer(-시도, names_to = "후보자명", values_to = "득표") %>% 
  mutate(후보자명 = str_remove(후보자명, pattern = "_[0-9]+$")) %>% 
  mutate(구분 = "대의원")

type_03_tbl <- type_03 %>% 
  pivot_longer(-시도, names_to = "후보자명", values_to = "득표") %>% 
  mutate(후보자명 = str_remove(후보자명, pattern = "_[0-9]+$")) %>% 
  mutate(구분 = "유선전화")

type_04_tbl <- type_04 %>% 
  pivot_longer(-시도, names_to = "후보자명", values_to = "득표") %>% 
  mutate(후보자명 = str_remove(후보자명, pattern = "_[0-9]+$")) %>% 
  mutate(구분 = "국민선거인단")

race_tbl <- bind_rows(type_01_tbl, type_02_tbl) %>% 
  bind_rows(type_03_tbl) %>% 
  bind_rows(type_04_tbl) %>% 
  mutate(득표 = as.numeric(득표)) %>% 
  mutate(후보자명 = factor(후보자명, levels = c("이재명", "이낙연", "박용진", "추미애"))) %>% 
  mutate(시도 = factor(시도, levels = c("대전충남", "세종충북", "대구경북", "강원", "1차 슈퍼데이", "광주전남", "전북", "제주", "부울경", "인천", "2차 슈퍼데이"))) %>% 
  mutate(당원여론 = ifelse(str_detect(시도, "슈퍼데이"), "국민선거인단", "당원"))


## 데이터 내보내기 -----------------------------------------

race_tbl %>% 
  write_rds(file = "data/race_tbl.rds")

electoral_college %>% 
  write_rds(file = "data/electoral_college.rds")
## 엑셀 데이터 정합성 확인 ---------------------------------

test_that("엑셀 데이터 정합성", {

  total_sum <- race_tbl %>% 
    summarise(합계 = sum(득표)) %>% 
    pull()

  expect_equal(total_sum, 993656  )
})
Test passed 😸
sido_fct <- c("대전충남", "세종충북", "대구경북", "강원", "광주전남", "전북", "제주", "부울경", "인천")
report_day <- "10월 4일"
file_date <- "20211004"

2.1 선거인단

library(reactable)

electoral_college %>% 
  reactable::reactable(
    filterable = FALSE,  defaultPageSize = 11,
    columns = list(
      선거인단 = colDef(format = colFormat(separators = TRUE, digits = 0))
    ),
    bordered = TRUE,
    highlight = TRUE
  )

2.2 득표

race_tbl %>% 
  pivot_wider(names_from = 후보자명, values_from = 득표) %>% 
  reactable::reactable(
    filterable = TRUE,  defaultPageSize = 9,
    columns = list(
      이재명 = colDef(format = colFormat(separators = TRUE, digits = 0)),
      이낙연 = colDef(format = colFormat(separators = TRUE, digits = 0)),
      박용진 = colDef(format = colFormat(separators = TRUE, digits = 0)),
      추미애 = colDef(format = colFormat(separators = TRUE, digits = 0))
    ),
    bordered = TRUE,
    highlight = TRUE
  )

3 모듈

3.1 gt

3.1.1 선거인단

library(gt)

electoral_college %>% 
  mutate(비율 = 선거인단 / sum(선거인단)) %>% 
  gt::gt(
    rowname_col = "시도"
    ) %>% 
      tab_header(
      title = md("**&#9755; 제20대 대통령 선거 &#9754;**"),
      subtitle = md("*민주당 경선: 선거인단*")
    ) %>% 
    tab_source_note(
      source_note = md("기준 시점: 2021년 10월 04일")
    ) %>% 
    tab_options(
      table.width = pct(50),
      heading.background.color = "#1E61B0", # R logo 파란색
      heading.title.font.size = "32px",
      column_labels.background.color = "#F7F7F7", # R logo 회색 
      column_labels.font.weight = "bold",
      stub.background.color = "#ffffff",
      stub.font.weight = "bold"
    ) %>% 
    cols_align(
      align = "center",
      columns = c(시도)
    ) %>% 
    fmt_number(
      columns = 선거인단,
      decimals = 0
    ) %>% 
    fmt_percent(
      columns = 비율,
      decimals = 1
    ) %>% 
    tab_style(
      style = list(
        cell_fill(color = "#4083ef"),
        cell_text(weight = "bold")
      ),
      locations = cells_body(columns = c(시도, 선거인단, 비율), 
                             rows = str_detect(시도, "슈퍼"))) %>% 
    grand_summary_rows(
      columns = c(선거인단),
      fns = list(
        합계 = ~sum(.)),
      formatter = fmt_number,
      use_seps = TRUE,
      decimals = 0
    ) %>% 
    grand_summary_rows(
      columns = c(비율),
      fns = list(
        합계 = ~sum(.)),
      formatter = fmt_percent,
      use_seps = TRUE,
      decimals = 1
    ) %>% 
    cols_width(
      시도 ~ px(90),
      선거인단 ~ px(100),
      비율 ~ px(100)
    )
☛ 제20대 대통령 선거 ☚
민주당 경선: 선거인단
선거인단 비율
대전충남 52,820 3.4%
세종충북 23,803 1.5%
대구경북 16,170 1.0%
강원 16,292 1.1%
1차 슈퍼데이 641,922 41.4%
광주전남 127,823 8.2%
전북 76,191 4.9%
제주 13,349 0.9%
부울경 62,098 4.0%
인천 22,818 1.5%
2차 슈퍼데이 496,339 32.0%
합계 1,549,625 100.0%
기준 시점: 2021년 10월 04일

3.1.2 누적 득표

race_tbl %>% 
  group_by(후보자명) %>%
  summarise(득표 = sum(득표)) %>% 
  ungroup() %>% 
  mutate(비율 = 득표 / sum(득표)) %>% 
  gt(rowname_col = "후보자명") %>% 
    tab_header(
      title = md("**&#9755; 제20대 대통령 선거 &#9754;**"),
      subtitle = md("*민주당 경선: 누적 득표수*")
    ) %>% 
    tab_source_note(
      source_note = md("기준 시점: 2021년 10월 04일")
    ) %>% 
    tab_options(
      table.width = pct(50),
      heading.background.color = "#1E61B0", # R logo 파란색
      heading.title.font.size = "32px",
      column_labels.background.color = "#F7F7F7", # R logo 회색 
      column_labels.font.weight = "bold",
      stub.background.color = "#ffffff",
      stub.font.weight = "bold"
    ) %>% 
    fmt_percent(
      columns = 비율,
      decimals = 1
    ) %>% 
    fmt_number(
      columns = 득표,
      decimals = 0
    ) %>% 
    grand_summary_rows(
      
      columns = c(득표),
      fns = list(
        합계 = ~sum(.)),
      formatter = fmt_number,
      use_seps = TRUE,
      decimals = 0
    ) %>% 
    grand_summary_rows(
      columns = c(비율),
      fns = list(
        합계 = ~sum(.)),
      formatter = fmt_percent,
      use_seps = TRUE,
      decimals = 1
    )  %>% 
    tab_style(
      style = list(
        cell_text(align = "center")
      ),
    locations = cells_stub(rows = TRUE)
  )
☛ 제20대 대통령 선거 ☚
민주당 경선: 누적 득표수
득표 비율
이재명 545,537 54.9%
이낙연 341,076 34.3%
박용진 16,185 1.6%
추미애 90,858 9.1%
합계 993,656 100.0%
기준 시점: 2021년 10월 04일

3.1.3 당원 vs 국민선거인단

library(janitor)

## janitor 득표와 득표율에 큰 숫자에 천단위 콤마 추가
add_comma_seprator <- function(x) {
  first_half  <- str_extract(x, pattern = "^(.+?)\\(") %>% parse_number %>% scales::comma()
  second_half <- str_extract(x, pattern = "\\((.*?)\\)")
  glue::glue("{first_half}\n{second_half}")
}

race_tbl %>% 
  group_by(당원여론, 후보자명) %>%
  summarise(득표 = sum(득표)) %>% 
  ungroup() %>% 
  pivot_wider(names_from = 후보자명, values_from = 득표) %>% 
  ## janitor table decoration -----------
  adorn_totals(where = c("row", "col"), name = "합계") %>% 
  # mutate_if(is.numeric, scales::comma) %>% 
  adorn_percentages(denominator = "all") %>% 
  adorn_pct_formatting(digits = 1) %>% 
  adorn_ns(position = "front") %>% 
  mutate_at(vars(이재명, 이낙연, 박용진, 추미애, 합계), .funs =  add_comma_seprator) %>% 

  gt(rowname_col = "당원여론") %>% 
    tab_header(
      title = md("**&#9755; 제20대 대통령 선거 &#9754;**"),
      subtitle = md("*민주당 경선: 당원/여론 누적 득표수*")
    ) %>% 
    tab_source_note(
      source_note = md("기준 시점: 2021년 10월 04일")
    ) %>% 
    tab_options(
      table.width = pct(100),
      heading.background.color = "#1E61B0", # R logo 파란색
      heading.title.font.size = "32px",
      column_labels.background.color = "#F7F7F7", # R logo 회색 
      column_labels.font.weight = "bold",
      stub.background.color = "#ffffff",
      stub.font.weight = "bold"
    ) %>% 
    # fmt_number(
    #   columns = c(이재명, 이낙연, 박용진, 추미애, 합계),
    #   decimals = 0
    # ) %>% 
    tab_style(
      style = list(
        cell_text(align = "center")
      ),
    locations = cells_stub(rows = TRUE)
    ) %>% 
    tab_style(
      style = list(
        cell_fill(color = "#3c8ae8"),
        cell_text(
          align = "center",
          size = "medium",
          weight = "bold")
        ),
    locations = cells_body(rows = "합계")) %>% 
    tab_style(
      style = list(
        cell_fill(color = "#3c8ae8"),
        cell_text(
          align = "center",
          size = "medium",
          weight = "bold")
        ),
    locations = cells_body(columns = "합계"))
☛ 제20대 대통령 선거 ☚
민주당 경선: 당원/여론 누적 득표수
이재명 이낙연 박용진 추미애 합계
국민선거인단 425,999 (42.9%) 255,343 (25.7%) 13,247 (1.3%) 75,209 (7.6%) 769,798 (77.5%)
당원 119,538 (12.0%) 85,733 (8.6%) 2,938 (0.3%) 15,649 (1.6%) 223,858 (22.5%)
합계 545,537 (54.9%) 341,076 (34.3%) 16,185 (1.6%) 90,858 (9.1%) 993,656 (100.0%)
기준 시점: 2021년 10월 04일

3.1.4 지역별 당원

race_tbl %>% 
  filter(!str_detect(시도, "슈퍼")) %>% 
  group_by(시도, 후보자명) %>%
  summarise(득표 = sum(득표)) %>% 
  ungroup() %>% 
  pivot_wider(names_from = 후보자명, values_from = 득표) %>% 
  ## janitor table decoration -----------
  adorn_totals(where = c("row", "col"), name = "합계") %>% 
  # mutate_if(is.numeric, scales::comma) %>% 
  adorn_percentages(denominator = "all") %>% 
  adorn_pct_formatting(digits = 1) %>% 
  adorn_ns(position = "front") %>% 
  mutate_at(vars(이재명, 이낙연, 박용진, 추미애, 합계), .funs =  add_comma_seprator)  %>%  
  gt(rowname_col = "시도") %>% 
    tab_header(
      title = md("**&#9755; 제20대 대통령 선거 &#9754;**"),
      subtitle = md("*민주당 경선: 시도별 누적 득표수*")
    ) %>% 
    tab_source_note(
      source_note = md("기준 시점: 2021년 10월 04일")
    ) %>% 
    tab_options(
      table.width = pct(80),
      heading.background.color = "#1E61B0", # R logo 파란색
      heading.title.font.size = "32px",
      column_labels.background.color = "#F7F7F7", # R logo 회색 
      column_labels.font.weight = "bold",
      stub.background.color = "#ffffff",
      stub.font.weight = "bold"
    ) %>% 
    # fmt_number(
    #   columns = c(이재명, 이낙연, 박용진, 추미애),
    #   decimals = 0
    # ) %>% 
    # grand_summary_rows(
    #   columns = c(이재명, 이낙연, 박용진, 추미애),
    #   fns = list(
    #     합계 = ~sum(.)),
    #   formatter = fmt_number,
    #   use_seps = TRUE,
    #   decimals = 0
    # ) %>% 
    tab_style(
      style = list(
        cell_text(align = "center")
      ),
    locations = cells_stub(rows = TRUE)
    ) %>% 
    tab_style(
      style = list(
        cell_fill(color = "#3c8ae8"),
        cell_text(
          align = "center",
          size = "medium",
          weight = "bold")
        ),
    locations = cells_body(rows = "합계")) %>% 
    tab_style(
      style = list(
        cell_fill(color = "#3c8ae8"),
        cell_text(
          align = "center",
          size = "medium",
          weight = "bold")
        ),
    locations = cells_body(columns = "합계"))
☛ 제20대 대통령 선거 ☚
민주당 경선: 시도별 누적 득표수
이재명 이낙연 박용진 추미애 합계
대전충남 14,012 (6.3%) 7,007 (3.1%) 624 (0.3%) 1,704 (0.8%) 23,347 (10.4%)
세종충북 7,035 (3.1%) 3,834 (1.7%) 287 (0.1%) 915 (0.4%) 12,071 (5.4%)
대구경북 5,999 (2.7%) 3,284 (1.5%) 137 (0.1%) 1,741 (0.8%) 11,161 (5.0%)
강원 5,048 (2.3%) 2,462 (1.1%) 173 (0.1%) 785 (0.4%) 8,468 (3.8%)
광주전남 33,726 (15.1%) 33,848 (15.1%) 471 (0.2%) 3,113 (1.4%) 71,158 (31.8%)
전북 22,276 (10.0%) 15,715 (7.0%) 512 (0.2%) 2,127 (1.0%) 40,630 (18.1%)
제주 3,944 (1.8%) 2,482 (1.1%) 69 (0.0%) 455 (0.2%) 6,950 (3.1%)
부울경 19,698 (8.8%) 11,969 (5.3%) 461 (0.2%) 3,468 (1.5%) 35,596 (15.9%)
인천 7,800 (3.5%) 5,132 (2.3%) 204 (0.1%) 1,341 (0.6%) 14,477 (6.5%)
합계 119,538 (53.4%) 85,733 (38.3%) 2,938 (1.3%) 15,649 (7.0%) 223,858 (100.0%)
기준 시점: 2021년 10월 04일

3.1.5 국민선거인단

race_tbl %>% 
  filter(str_detect(시도, "슈퍼")) %>% 
  group_by(시도, 후보자명) %>%
  summarise(득표 = sum(득표)) %>% 
  ungroup() %>% 
  pivot_wider(names_from = 후보자명, values_from = 득표) %>%
  ## janitor table decoration -----------
  adorn_totals(where = c("row", "col"), name = "합계") %>% 
  # mutate_if(is.numeric, scales::comma) %>% 
  adorn_percentages(denominator = "all") %>% 
  adorn_pct_formatting(digits = 1) %>% 
  adorn_ns(position = "front") %>% 
  mutate_at(vars(이재명, 이낙연, 박용진, 추미애, 합계), .funs =  add_comma_seprator)  %>%  
  
  gt(rowname_col = "시도") %>% 
    tab_header(
      title = md("**&#9755; 제20대 대통령 선거 &#9754;**"),
      subtitle = md("*민주당 경선: 국민선거인단 누적 득표수*")
    ) %>% 
    tab_source_note(
      source_note = md("기준 시점: 2021년 10월 04일")
    ) %>% 
    tab_options(
      table.width = pct(100),
      heading.background.color = "#1E61B0", # R logo 파란색
      heading.title.font.size = "32px",
      column_labels.background.color = "#F7F7F7", # R logo 회색 
      column_labels.font.weight = "bold",
      stub.background.color = "#ffffff",
      stub.font.weight = "bold"
    ) %>% 
    # fmt_number(
    #   columns = c(이재명, 이낙연, 박용진, 추미애),
    #   decimals = 0
    # ) %>% 
    # grand_summary_rows(
    #   columns = c(이재명, 이낙연, 박용진, 추미애),
    #   fns = list(
    #     합계 = ~sum(.)),
    #   formatter = fmt_number,
    #   use_seps = TRUE,
    #   decimals = 0
    # ) %>% 
    tab_style(
      style = list(
        cell_text(align = "center")
      ),
    locations = cells_stub(rows = TRUE)
    ) %>% 
    tab_style(
      style = list(
        cell_fill(color = "#3c8ae8"),
        cell_text(
          align = "center",
          size = "medium",
          weight = "bold")
        ),
    locations = cells_body(rows = "합계")) %>% 
    tab_style(
      style = list(
        cell_fill(color = "#3c8ae8"),
        cell_text(
          align = "center",
          size = "medium",
          weight = "bold")
        ),
    locations = cells_body(columns = "합계"))  
☛ 제20대 대통령 선거 ☚
민주당 경선: 국민선거인단 누적 득표수
이재명 이낙연 박용진 추미애 합계
1차 슈퍼데이 253,762 (33.0%) 156,203 (20.3%) 5,742 (0.7%) 57,977 (7.5%) 473,684 (61.5%)
2차 슈퍼데이 172,237 (22.4%) 99,140 (12.9%) 7,505 (1.0%) 17,232 (2.2%) 296,114 (38.5%)
합계 425,999 (55.3%) 255,343 (33.2%) 13,247 (1.7%) 75,209 (9.8%) 769,798 (100.0%)
기준 시점: 2021년 10월 04일

3.2 ggplot 그래프

3.2.1 선거인단

library(patchwork)

# 선거인단 ----------------
votes_tbl <- race_tbl %>% 
  group_by(시도) %>% 
  summarise(총투표수 = sum(득표))

electoral_g <- electoral_college %>% 
  left_join(votes_tbl) %>% 
  pivot_longer(-시도) %>%  
  mutate(시도 = factor(시도, levels = c("대전충남", "세종충북", "대구경북", "강원", "1차 슈퍼데이", "광주전남", "전북", "제주", "부울경", "인천", "2차 슈퍼데이"))) %>% 
  ggplot(aes(x = 시도, y = value, fill = name, width = ifelse(name == "총투표수", 0.3, 0.5))) +
    geom_col(stat = "identity") +
    theme_bw(base_family = "NanumGothic") +
    theme(legend.position = "right",
          axis.text.y = element_text(size = rel(1.5), colour = "gray35", family = "NanumBarunpen", face="bold"),
          axis.text.x = element_text(size = rel(1.0), colour = "black", family = "NanumBarunpen", face="bold",
                                     angle = 15, vjust = 0.5, hjust=0.5),
          strip.background=element_rect(fill="gray95"),
          plot.title=element_text(size=18, face="bold", family = "NanumBarunpen"),
          plot.subtitle=element_text(face="bold", size=13, colour="grey10", family = "NanumBarunpen")) +
    labs(x        = "",
         y        = "",
         title    = "제20대 대통령 선거 / 민주당 경선",
         subtitle = glue::glue("{report_day} 기준 선거인단과 총투표수"),
         caption  = "자료 출처: 더불어 민주당 보도자료, https://theminjoo.kr/board/lists/presskit",
         fill     = "") +
    scale_y_continuous(labels = scales::comma) +
    # coord_flip() +
    scale_fill_manual(values = c("gray77", "darkblue"))

electoral_g

3.2.2 누적 득표수

## 누적 ----------
race_sum_tbl <- race_tbl %>% 
  group_by(후보자명) %>% 
  summarise(누적득표 = sum(득표)) %>% 
  arrange(-누적득표)

cumsum_g <- race_sum_tbl %>% 
  mutate(후보자명 = factor(후보자명, levels = c("이재명", "이낙연", "박용진", "추미애"))) %>% 
  ggplot(aes(x = 후보자명, y = 누적득표, fill = 후보자명)) +
    geom_col(width = 0.3) +
    theme_bw(base_family = "NanumGothic") +
    theme(legend.position = "none",
          axis.text.y = element_text(size = rel(1.5), colour = "gray35", family = "NanumBarunpen", face="bold"),
          axis.text.x = element_text(size = rel(2.0), colour = "black", family = "NanumBarunpen", face="bold"),
          strip.background=element_rect(fill="gray95"),
          plot.title=element_text(size=18, face="bold", family = "NanumBarunpen"),
          plot.subtitle=element_text(face="bold", size=13, colour="grey10", family = "NanumBarunpen")) +
    labs(x = "",
         y = "",
         title = "제20대 대통령 선거 / 민주당 경선",
         subtitle = glue::glue("{report_day} 기준 누적 득표수"),
         caption = "자료 출처: 더불어 민주당 보도자료, https://theminjoo.kr/board/lists/presskit") +
    scale_y_continuous(labels = scales::comma) +
    scale_fill_manual(values = c("blue", "red", "gray", "green"))

cumsum_g

# # 총괄 요약 ------------------------------------------
# extrafont::loadfonts()
# 
# (cumsum_g +
#     labs(title = "",
#          subtitle = "누적 득표수",
#          caption = "")) +
# (electoral_g +
#   labs(title = "",
#        subtitle = "선거인단과 총투표수",
#        caption = "") ) +
#   plot_annotation(title = '제20대 대통령 선거 / 민주당 경선') &
#   theme_bw(base_family = "NanumBarunpen") +
#   theme(legend.position = "none",
#           axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

3.2.3 당원 국민선거인단

cumsum_two_g <- race_tbl %>% 
  group_by(당원여론, 후보자명) %>% 
  summarise(득표 = sum(득표)) %>% 
  ungroup() %>% 
  pivot_wider(names_from = 당원여론, values_from = 득표) %>% 
  mutate(누적득표 = 국민선거인단  + 당원) %>% 
  pivot_longer(국민선거인단:누적득표, names_to = "구분", values_to = "득표") %>% 
  mutate(후보자명 = factor(후보자명, levels = c("이재명", "김두관", "정세균", "이낙연", "박용진", "추미애")),
         구분     = factor(구분, levels = c("누적득표", "국민선거인단", "당원"))) %>% 
  ggplot(aes(x = 후보자명, y = 득표, fill = 후보자명)) +
    geom_col(width = 0.5) +
    facet_wrap(~구분) +
    theme_bw(base_family = "NanumGothic") +
    theme(legend.position = "none",
          strip.text.x = element_text(size = rel(1.7), colour = "black", family = "NanumMyeongjo", face="bold"),
          axis.text.y = element_text(size = rel(1.5), colour = "gray35", family = "NanumBarunpen", face="bold"),
          axis.text.x = element_text(size = rel(1.3), colour = "black", family = "NanumBarunpen", face="bold"),
          strip.background=element_rect(fill="gray95"),
          plot.title=element_text(size=18, face="bold", family = "NanumBarunpen"),
          plot.subtitle=element_text(face="bold", size=13, colour="grey10", family = "NanumBarunpen")) +
    labs(x = "",
         y = "",
         title = "제20대 대통령 선거 / 민주당 경선",
         subtitle = "국민선거인단, 당원 누적 득표수") +
    scale_y_continuous(labels = scales::comma) +
    scale_fill_manual(values = c("blue", "red", "gray", "green"))  

cumsum_two_g

3.2.4 지역별 득표수

## 누적 ----------
sido_g <- race_tbl %>% 
  filter(!str_detect(시도, "슈퍼")) %>% 
  ggplot(aes(x = 후보자명, y = 득표, fill = 후보자명)) +
    geom_col() +
    facet_wrap(~시도) +
    theme_bw(base_family = "NanumGothic") +
    theme(legend.position = "none",
          strip.text.x = element_text(size = rel(1.1), colour = "black", family = "NanumMyeongjo", face="bold"),
          axis.text.y = element_text(size = rel(1.5), colour = "gray35", family = "NanumBarunpen", face="bold"),
          axis.text.x = element_text(size = rel(1.3), colour = "black", family = "NanumBarunpen", face="bold"),
          strip.background=element_rect(fill="gray95"),
          plot.title=element_text(size=18, face="bold", family = "NanumBarunpen"),
          plot.subtitle=element_text(face="bold", size=13, colour="grey10", family = "NanumBarunpen")) +
    labs(x = "",
         y = "",
         title = "제20대 대통령 선거 / 민주당 경선",
         subtitle = "지역순회 시도별 득표수") +
    scale_y_continuous(labels = scales::comma) +
    scale_fill_manual(values = c("blue", "gray", "gray", "red", "gray", "green"))

sido_g

3.2.5 국민선거인단

## 누적 ----------
people_g <- race_tbl %>% 
  filter(str_detect(시도, "슈퍼")) %>% 
  ggplot(aes(x = 후보자명, y = 득표, fill = 후보자명)) +
    geom_col(width = 0.5) +
    facet_wrap(~시도) +
    theme_bw(base_family = "NanumGothic") +
    theme(legend.position = "none",
          strip.text.x = element_text(size = rel(1.7), colour = "black", family = "NanumMyeongjo", face="bold"),
          axis.text.y = element_text(size = rel(1.5), colour = "gray35", family = "NanumBarunpen", face="bold"),
          axis.text.x = element_text(size = rel(1.3), colour = "black", family = "NanumBarunpen", face="bold"),
          strip.background=element_rect(fill="gray95"),
          plot.title=element_text(size=18, face="bold", family = "NanumBarunpen"),
          plot.subtitle=element_text(face="bold", size=13, colour="grey10", family = "NanumBarunpen")) +
    labs(x = "",
         y = "",
         title = "제20대 대통령 선거 / 민주당 경선",
         subtitle = "국민선거인단 득표수") +
    scale_y_continuous(labels = scales::comma) +
    scale_fill_manual(values = c("blue", "red", "gray", "green"))

people_g

 

데이터 과학자 이광춘 저작

kwangchun.lee.7@gmail.com